package org.flexspeedup.blog.controllers
{
import flash.events.IEventDispatcher;

import mx.collections.ArrayCollection;
import mx.rpc.AsyncToken;
import mx.rpc.Responder;
import mx.rpc.remoting.RemoteObject;

import org.flexspeedup.blog.events.ShowPopUpEvent;
import org.flexspeedup.blog.models.PopUpModel;
import org.flexspeedup.blog.models.ResultSet;
import org.flexspeedup.blog.types.ServiceType;
import org.flexspeedup.model.IListModel;
import org.flexspeedup.model.IResultSet;
import org.flexspeedup.model.ListModel;
import org.flexspeedup.utils.SwizDispatcherBase;

/**
 * Template implementation of IBlogController
 */
public class BlogController extends SwizDispatcherBase implements IBlogController
{

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new <code>BlogController</code> instance.
     */
    public function BlogController()
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  blogModel
    //----------------------------------
    /**
     *  @private
     */    
    protected var _blogModel:IListModel;
    
    [Bindable]
    /**
     * @inheritDoc
     */
    public function get blogModel():IListModel
    {
        return _blogModel;
    }
    
    [Inject(source="blogModel")]  
    public function set blogModel(value:IListModel):void
    {
        _blogModel = value;
    }

    //----------------------------------
    //  blogService
    //----------------------------------
    /**
     *  @private
     */    
    protected var _blogService:RemoteObject;
    
    [Bindable]
    /**
     * @inheritDoc
     */
    public function get blogService():RemoteObject
    {
        return _blogService;
    }
    
    [Inject(source="blogService")]
    public function set blogService(value:RemoteObject):void
    {
        _blogService = value;
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  getPosts
    //----------------------------------
    /**z
     * @inheritDoc
     */
    public function getPosts(page:int=1, pageSize:int=10, category:String = null):void
    {
        var token:AsyncToken = blogService.getOperation(
            ServiceType.GET_POSTS).send(page, pageSize, category);

        token.addResponder
        (
            new Responder
                (
                    getPosts_resultHandler,
                    getPosts_faultHandler
                )
        );
    }

    //--------------------------------------------------------------------------
    //
    //  Event handlers
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  getPosts_resultHandler
    //----------------------------------
    /**
     * @private
     */
    private function getPosts_resultHandler(data:Object):void
    {
        blogModel.dataProvider = new ArrayCollection(data.result.items);  
        blogModel.resultSet = data.result as ResultSet;
    }    
    
    //----------------------------------
    //  getPosts_faultHandler
    //----------------------------------
    /**
     * @private
     */
    private function getPosts_faultHandler(info:Object):void
    {
        faultHandler(info);
    }

    //----------------------------------
    //  faultHandler
    //----------------------------------
    /**
     * @private
     */
    private function faultHandler(info:Object):void
    {
        var popUpModel:PopUpModel = new PopUpModel
        (
            "error",
            "there was an error processing your request, please try again"
        );
        dispatchEvent(new ShowPopUpEvent(popUpModel));
    } 

}
}